home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / STL / Slides / STL9.cp < prev   
Text File  |  1998-06-18  |  841b  |  31 lines

  1. // STL9.cp
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string    a = "Standard strings are easy to initialize.";
  9.     a += " And they can grow without end as needed.";
  10.     string::iterator    iter = find(a.begin(), a.end(), 'A');
  11.     
  12.     if (iter != a.end())
  13.     {
  14.         size_t    offset = iter - a.begin();
  15.         a[offset] = 'a';    // They use array notation like C strings.
  16.     }
  17.     iter = find(a.begin(), a.end(), '.');
  18.     if (iter != a.end())
  19.     {
  20.         a.erase(iter);    // They are STL containers.
  21.     }
  22.     cout << a << endl;
  23.     cout << "Size is ";
  24.     // cout << strlen(a);        // No coercion to char *.
  25.     cout << strlen(a.c_str());    // But it easy to get a char *.
  26.     cout << "." << endl;
  27.     // They automagically delete their allocated memory when they go out of scope.
  28. }
  29. // Standard strings are easy to initialize and they can grow without end as needed.
  30. // Size is 80.
  31.